import request from '@/utils/request'

//获取最新九个商品接口,首页渲染
export const getGoodsdata = () => {
  return request.get('/goods/getNewGoods', {})
}
//根据商品id查商品信息接口
export const getGoodsid = (id) => {
  return request.get(`/goods/getById/${id}`, id)
}
//购买商品接口
export const buygoodService = (id) => {
  return request.post(`/goods/buy/${id}`, { description: '11' })
}
//查看自己商品的接口
export const getOwngoods = () => {
  return request.get('/goods/getMyGoods')
}
// 新增商品接口
export const addOwngoods = ({
  name,
  price,
  categoryId,
  image,
  description
}) => {
  return request.post('/goods/addGoods', {
    name,
    price,
    categoryId,
    image,
    description
  })
}
//上传照片接口
export const upimage = (file) => {
  // 创建一个 FormData 实例
  const formData = new FormData()
  // 假设 avatar 是一个文件对象，比如来自 <input type="file"> 的 File 对象
  // console.log(typeof file)
  formData.append('file', file)
  // console.log(formData)
  return request.post('/common/upload', formData)
}
//删除商品接口
export const deleteowngoods = (id) => {
  return request.delete(`/goods/delete/${id}`)
}
//修改商品信息接口
export const changegoods = ({
  id,
  name,
  price,
  categoryId,
  image,
  description
}) => {
  return request.put('/goods/changeGoods', {
    id,
    name,
    price,
    categoryId,
    image,
    description
  })
}
//分类查询接口
export const searchCategory = () => {
  return request.get('/category/getCategory')
}
//根据分类id分页查询商品信息
export const searchIDgoods = (categoryId, params) => {
  return request.get(`/goods/page/${categoryId}`, { params })
}
//Banner分类接口
export const searchBanner = (categoryId) => {
  return request.get(`/goods/home/${categoryId}`)
}
//修改商品接口
export const changeOwnsGoods = ({
  id,
  name,
  price,
  categoryId,
  image,
  description
}) => {
  return request.put('/goods/changeGoods', {
    id,
    name,
    price,
    categoryId,
    image,
    description
  })
}
//新增分类
export const addcategory = ({ name, sort, image, description }) => {
  return request.post('/category/addCategory', {
    name,
    sort,
    image,
    description
  })
}
//删除分类
export const deletecategory = (id) => {
  return request.delete(`/category/delete/${id}`)
}
//用户推荐商品接口
export const getRecGoods = () => {
  return request.get('/goods/getRecGoods')
}
//根据名称模糊查找接口
export const searchByName = ({ name, pageNum, pageSize, categoryId }) => {
  return request.post('/goods/searchByName', {
    name,
    pageNum,
    pageSize,
    categoryId
  })
}
